﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO;
using System.Threading;
using System.Media;
namespace SuDokuSolution
{
	/// <summary>
	/// Summary description for MainForm.
	/// </summary>
	public class MainForm : System.Windows.Forms.Form
	{
        public TimeSpan one2 = default(TimeSpan);
        public TimeSpan two;
        private IContainer components;
		private System.Windows.Forms.MenuItem menuItem12;
		private System.Windows.Forms.MenuItem menuItem14;
		private System.Windows.Forms.MenuItem menuItem18;
		private System.Windows.Forms.MenuItem menuItem22;
		private System.Windows.Forms.MenuItem fileItem;
		private System.Windows.Forms.MenuItem newItem;
		private System.Windows.Forms.MenuItem autoGenerateItem;
		private System.Windows.Forms.MenuItem manuallyGenearteItem;
		private System.Windows.Forms.MenuItem saveItem;
		private System.Windows.Forms.MenuItem loadItem;
		private System.Windows.Forms.MenuItem saveImageItem;
		private System.Windows.Forms.MenuItem exitItem;
		private System.Windows.Forms.MenuItem toolsItem;
		private System.Windows.Forms.MenuItem candidateItem;
		private System.Windows.Forms.MenuItem simultaneousItem;
		private System.Windows.Forms.MenuItem showsolutionItem;
		private System.Windows.Forms.MenuItem clearSolutionItem;
		private System.Windows.Forms.MenuItem aboutItem;
		private System.Windows.Forms.MenuItem eraseItem;

		private GridControl_4_by_4 gridControl;
        private GridControl_9_by_9 gridControl1;
        private Constants one = new Constants();
        private Constants one1 = new Constants1();
		private System.Windows.Forms.MenuItem removeAllItem;
		private System.Windows.Forms.MenuItem autoUpdateItem;
		internal System.Windows.Forms.MenuItem autoFillItem;
		private System.Windows.Forms.SaveFileDialog saveFileDialog;
        private System.Windows.Forms.MenuItem menuItem1;
        private System.Windows.Forms.MenuItem showNextItem;
        private MenuItem menuItem3;
        private System.Windows.Forms.Timer timer1;
        private Label label1;
		private System.Windows.Forms.Button doneButton;
        private MenuItem menuItem4;
        private MenuItem menuItem5;
        private MenuItem menuItem8;
        private MenuItem menuItem2;
        SoundPlayer player = new SoundPlayer(@"C:\Users\Саша\Music\frank_ocean_-_lost_zaycev_net.wav");

		//#endregion Declarations

		public MainForm()
		{
           // timer1.Enabled = false;
            InitializeComponent();
           
			
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            this.components = new System.ComponentModel.Container();
            System.Windows.Forms.MainMenu mainMenu1;
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm));
            this.fileItem = new System.Windows.Forms.MenuItem();
            this.newItem = new System.Windows.Forms.MenuItem();
            this.autoGenerateItem = new System.Windows.Forms.MenuItem();
            this.menuItem4 = new System.Windows.Forms.MenuItem();
            this.menuItem5 = new System.Windows.Forms.MenuItem();
            this.manuallyGenearteItem = new System.Windows.Forms.MenuItem();
            this.menuItem12 = new System.Windows.Forms.MenuItem();
            this.saveItem = new System.Windows.Forms.MenuItem();
            this.loadItem = new System.Windows.Forms.MenuItem();
            this.saveImageItem = new System.Windows.Forms.MenuItem();
            this.menuItem14 = new System.Windows.Forms.MenuItem();
            this.exitItem = new System.Windows.Forms.MenuItem();
            this.toolsItem = new System.Windows.Forms.MenuItem();
            this.candidateItem = new System.Windows.Forms.MenuItem();
            this.autoFillItem = new System.Windows.Forms.MenuItem();
            this.removeAllItem = new System.Windows.Forms.MenuItem();
            this.menuItem18 = new System.Windows.Forms.MenuItem();
            this.autoUpdateItem = new System.Windows.Forms.MenuItem();
            this.simultaneousItem = new System.Windows.Forms.MenuItem();
            this.menuItem22 = new System.Windows.Forms.MenuItem();
            this.showNextItem = new System.Windows.Forms.MenuItem();
            this.showsolutionItem = new System.Windows.Forms.MenuItem();
            this.clearSolutionItem = new System.Windows.Forms.MenuItem();
            this.eraseItem = new System.Windows.Forms.MenuItem();
            this.menuItem3 = new System.Windows.Forms.MenuItem();
            this.menuItem1 = new System.Windows.Forms.MenuItem();
            this.aboutItem = new System.Windows.Forms.MenuItem();
            this.menuItem8 = new System.Windows.Forms.MenuItem();
            this.menuItem2 = new System.Windows.Forms.MenuItem();
            this.saveFileDialog = new System.Windows.Forms.SaveFileDialog();
            this.doneButton = new System.Windows.Forms.Button();
            this.timer1 = new System.Windows.Forms.Timer(this.components);
            this.label1 = new System.Windows.Forms.Label();
            mainMenu1 = new System.Windows.Forms.MainMenu(this.components);
            this.SuspendLayout();
            // 
            // mainMenu1
            // 
            mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.fileItem,
            this.toolsItem,
            this.menuItem3,
            this.menuItem1,
            this.menuItem2});
            // 
            // fileItem
            // 
            this.fileItem.Index = 0;
            this.fileItem.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.newItem,
            this.menuItem12,
            this.saveItem,
            this.loadItem,
            this.saveImageItem,
            this.menuItem14,
            this.exitItem});
            this.fileItem.Text = "&File";
            this.fileItem.Click += new System.EventHandler(this.fileItem_Click);
            // 
            // newItem
            // 
            this.newItem.Index = 0;
            this.newItem.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.autoGenerateItem,
            this.manuallyGenearteItem});
            this.newItem.Text = "&New";
            // 
            // autoGenerateItem
            // 
            this.autoGenerateItem.Index = 0;
            this.autoGenerateItem.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.menuItem4,
            this.menuItem5});
            this.autoGenerateItem.Text = "&Auto Generate";
            // 
            // menuItem4
            // 
            this.menuItem4.Index = 0;
            this.menuItem4.Text = "Standart";
            this.menuItem4.Click += new System.EventHandler(this.menuItem4_Click);
            // 
            // menuItem5
            // 
            this.menuItem5.Index = 1;
            this.menuItem5.Text = "Kids";
            this.menuItem5.Click += new System.EventHandler(this.menuItem5_Click_1);
            // 
            // manuallyGenearteItem
            // 
            this.manuallyGenearteItem.Index = 1;
            this.manuallyGenearteItem.Text = "&Personal_Game";
            this.manuallyGenearteItem.Click += new System.EventHandler(this.manuallyGenearteItem_Click);
            // 
            // menuItem12
            // 
            this.menuItem12.Index = 1;
            this.menuItem12.Text = "-";
            // 
            // saveItem
            // 
            this.saveItem.Index = 2;
            this.saveItem.Text = "&Save Sudoku";
            this.saveItem.Click += new System.EventHandler(this.saveItem_Click);
            // 
            // loadItem
            // 
            this.loadItem.Index = 3;
            this.loadItem.Text = "&Load  Sudoku";
            this.loadItem.Click += new System.EventHandler(this.loadItem_Click);
            // 
            // saveImageItem
            // 
            this.saveImageItem.Index = 4;
            this.saveImageItem.Text = "S&ave Sudoku Image";
            this.saveImageItem.Click += new System.EventHandler(this.saveImageItem_Click);
            // 
            // menuItem14
            // 
            this.menuItem14.Index = 5;
            this.menuItem14.Text = "-";
            // 
            // exitItem
            // 
            this.exitItem.Index = 6;
            this.exitItem.Shortcut = System.Windows.Forms.Shortcut.AltF4;
            this.exitItem.Text = "E&xit";
            this.exitItem.Click += new System.EventHandler(this.exitItem_Click);
            // 
            // toolsItem
            // 
            this.toolsItem.Index = 1;
            this.toolsItem.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.candidateItem,
            this.simultaneousItem,
            this.menuItem22,
            this.showNextItem,
            this.showsolutionItem,
            this.clearSolutionItem,
            this.eraseItem});
            this.toolsItem.Text = "Tools";
            // 
            // candidateItem
            // 
            this.candidateItem.Index = 0;
            this.candidateItem.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.autoFillItem,
            this.removeAllItem,
            this.menuItem18,
            this.autoUpdateItem});
            this.candidateItem.Text = "&Candidate Numbers";
            // 
            // autoFillItem
            // 
            this.autoFillItem.Index = 0;
            this.autoFillItem.Text = "&Auto Fill";
            this.autoFillItem.Click += new System.EventHandler(this.autoFillItem_Click);
            // 
            // removeAllItem
            // 
            this.removeAllItem.Index = 1;
            this.removeAllItem.Text = "&Remove All";
            this.removeAllItem.Click += new System.EventHandler(this.removeAllItem_Click);
            // 
            // menuItem18
            // 
            this.menuItem18.Index = 2;
            this.menuItem18.Text = "-";
            // 
            // autoUpdateItem
            // 
            this.autoUpdateItem.Index = 3;
            this.autoUpdateItem.Text = "Auto &Update";
            this.autoUpdateItem.Click += new System.EventHandler(this.autoUpdateItem_Click);
            // 
            // simultaneousItem
            // 
            this.simultaneousItem.Index = 1;
            this.simultaneousItem.Text = "&Simultaneous Check";
            this.simultaneousItem.Click += new System.EventHandler(this.simultaneousItem_Click);
            // 
            // menuItem22
            // 
            this.menuItem22.Index = 2;
            this.menuItem22.Text = "-";
            // 
            // showNextItem
            // 
            this.showNextItem.Index = 3;
            this.showNextItem.Shortcut = System.Windows.Forms.Shortcut.CtrlM;
            this.showNextItem.Text = "Auto Play Next Step";
            this.showNextItem.Click += new System.EventHandler(this.showNextItem_Click);
            // 
            // showsolutionItem
            // 
            this.showsolutionItem.Index = 4;
            this.showsolutionItem.Text = "&Show Solution";
            this.showsolutionItem.Click += new System.EventHandler(this.showsolutionItem_Click);
            // 
            // clearSolutionItem
            // 
            this.clearSolutionItem.Index = 5;
            this.clearSolutionItem.Text = "&Clear Solution";
            this.clearSolutionItem.Click += new System.EventHandler(this.clearSolutionItem_Click);
            // 
            // eraseItem
            // 
            this.eraseItem.Index = 6;
            this.eraseItem.Text = "&Erase All";
            this.eraseItem.Click += new System.EventHandler(this.eraseItem_Click);
            // 
            // menuItem3
            // 
            this.menuItem3.Index = 2;
            this.menuItem3.Text = "End Game";
            this.menuItem3.Click += new System.EventHandler(this.menuItem3_Click);
            // 
            // menuItem1
            // 
            this.menuItem1.Index = 3;
            this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.aboutItem,
            this.menuItem8});
            this.menuItem1.Text = "&Help";
            // 
            // aboutItem
            // 
            this.aboutItem.Index = 0;
            this.aboutItem.Text = "&About";
            this.aboutItem.Click += new System.EventHandler(this.aboutItem_Click);
            // 
            // menuItem8
            // 
            this.menuItem8.Index = 1;
            this.menuItem8.Text = "Instruction";
            // 
            // menuItem2
            // 
            this.menuItem2.Index = 4;
            this.menuItem2.Text = "Check";
            this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
            // 
            // doneButton
            // 
            this.doneButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.doneButton.Location = new System.Drawing.Point(599, 0);
            this.doneButton.Name = "doneButton";
            this.doneButton.Size = new System.Drawing.Size(56, 23);
            this.doneButton.TabIndex = 0;
            this.doneButton.Text = "Done";
            this.doneButton.Visible = false;
            this.doneButton.Click += new System.EventHandler(this.doneButton_Click);
            // 
            // timer1
            // 
            this.timer1.Enabled = true;
            this.timer1.Interval = 1000;
            this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.BackColor = System.Drawing.Color.WhiteSmoke;
            this.label1.Font = new System.Drawing.Font("Mistral", 20.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(204)));
            this.label1.ForeColor = System.Drawing.SystemColors.InfoText;
            this.label1.Location = new System.Drawing.Point(301, 0);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(0, 33);
            this.label1.TabIndex = 2;
            // 
            // MainForm
            // 
            this.AllowDrop = true;
            this.AutoScaleBaseSize = new System.Drawing.Size(4, 14);
            this.ClientSize = new System.Drawing.Size(655, 494);
            this.Controls.Add(this.doneButton);
            this.Controls.Add(this.label1);
            this.DoubleBuffered = true;
            this.Font = new System.Drawing.Font("Mistral", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204)));
            this.ForeColor = System.Drawing.SystemColors.ControlText;
            this.HelpButton = true;
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.Menu = mainMenu1;
            this.Name = "MainForm";
            this.RightToLeftLayout = true;
            this.Text = "SuDoKu-game";
            this.TopMost = true;
            this.Closing += new System.ComponentModel.CancelEventHandler(this.MainForm_Closing);
            this.Load += new System.EventHandler(this.MainForm_Load);
            this.ResumeLayout(false);
            this.PerformLayout();

		}
		#endregion

		[STAThread]
        public static void Main()
		{
			try
			{
				Application.Run(new MainForm());
			}
			catch(Exception e)
			{
				MessageBox.Show(e.Message);
			}
		}

		protected override void OnResize(EventArgs e)
		{
			base.OnResize (e);
			if(gridControl!=null)
			{
				gridControl.Location=new Point(8,8);
				gridControl.Size=new Size(ClientSize.Width-16, ClientSize.Height-32);
			}
		}

		

		private void autoFillItem_Click(object sender, System.EventArgs e)
		{
            if (gridControl1 !=null)
            {
                for (int row = 0; row < one1.nbOfCells; row++)
                {
                    for (int col = 0; col < one1.nbOfCells; col++)
                    {
                        int Count = this.gridControl1.cellGrid[row, col].cellData.candidateNb.Count;
                        this.gridControl1.cellGrid[row, col].CheckedNbs.Clear();
                        for (int i = 0; i < Count; i++)
                        {
                            this.gridControl1.cellGrid[row, col].CheckedNbs.Add(
                                this.gridControl1.cellGrid[row, col].cellData.candidateNb[i]);
                        }
                        this.gridControl1.RedrawCell(this.gridControl1.cellGrid[row, col]);
                    }
                }
            }
           
		}

		private void autoUpdateItem_Click(object sender, System.EventArgs e)
		{
            if (gridControl1 != null)
            {
                gridControl1.autoUpdate = !gridControl1.autoUpdate;
                if (gridControl1.autoUpdate)
                {
                    this.autoUpdateItem.Checked = true;
                    this.simultaneousItem.Checked = true;
                    this.gridControl1.simultaneousCheck = true;
                }
                else
                    this.autoUpdateItem.Checked = false;
            }
            
		}

		private void removeAllItem_Click(object sender, System.EventArgs e)
		{
         
            if (gridControl1 != null)
            {
                for (int row = 0; row < one1.nbOfCells; row++)
                {
                    for (int col = 0; col < one1.nbOfCells; col++)
                    {
                        this.gridControl1.cellGrid[row, col].CheckedNbs.Clear();
                        this.gridControl1.RedrawCell(this.gridControl1.cellGrid[row, col]);
                    }
                }
            }
		}

		private void simultaneousItem_Click(object sender, System.EventArgs e)
		{
            if (gridControl1 != null)
            {
                this.gridControl1.simultaneousCheck = !gridControl1.simultaneousCheck;
                if (gridControl1.simultaneousCheck)
                    simultaneousItem.Checked = true;
                else
                    simultaneousItem.Checked = false;
            }
            
		}

        private void showsolutionItem_Click(object sender, System.EventArgs e)
        {
            if (gridControl1 != null)
            {
                for (int row = 0; row < one1.nbOfCells; row++)
                {
                    for (int col = 0; col < one1.nbOfCells; col++)
                    {
                        gridControl1.cellGrid[row, col].cellData.problemNb =
                            gridControl1.cellGrid[row, col].cellData.originalNb;
                        gridControl1.RedrawCell(gridControl1.cellGrid[row, col]);
                    }
                }
            }

        }

		private void clearSolutionItem_Click(object sender, System.EventArgs e)
		{
        
            if (gridControl1 != null)
            {
                for (int row = 0; row < one1.nbOfCells; row++)
                {
                    for (int col = 0; col < one1.nbOfCells; col++)
                    {
                        if (!gridControl1.cellGrid[row, col].cellData.readOnly)
                        {
                            gridControl1.cellGrid[row, col].cellData.problemNb = one1.NoNumber;
                            gridControl1.cellGrid[row, col].CheckedNbs.Clear();
                            this.gridControl1.RedrawCell(this.gridControl1.cellGrid[row, col]);
                        }
                    }
                }
                for (int row = 0; row < one1.nbOfCells; row++)
                {
                    for (int col = 0; col < one1.nbOfCells; col++)
                    {
                        gridControl1.searchCandidates(row, col);
                    }
                }
            }
		}

		private void eraseItem_Click(object sender, System.EventArgs e)
		{
           
            if (gridControl1 != null)
            {
                for (int row = 0; row < one1.nbOfCells; row++)
                {
                    for (int col = 0; col < one1.nbOfCells; col++)
                    {
                        gridControl1.cellGrid[row, col].cellData.problemNb =
                            gridControl1.cellGrid[row, col].cellData.originalNb = one1.NoNumber;
                        gridControl1.cellGrid[row, col].cellData.candidateNb.Clear();
                        gridControl1.cellGrid[row, col].CheckedNbs.Clear();
                        gridControl1.Enabled = false;
                        this.gridControl1.RedrawCell(this.gridControl1.cellGrid[row, col]);
                    }
                }
                changeMenuState(false);
            }
		}
		private void EndGame(object sender,EventArgs e)
		{
            int row, col;
            if (gridControl != null)
            {
                if (!gridControl.varify(out  row, out col))
                {
                    MessageBox.Show("Wrong solution. Entry at " + row + " ," + col + " needs a check", "ERROR");
                }
                else
                {
                    timer1.Enabled = false;
                    MessageBox.Show("Congratulations! Success Your time is  " + one2.ToString());


                }
            }
            if (gridControl1 != null)
            {
                if (!gridControl1.varify(out  row, out col))
                {
                    MessageBox.Show("Wrong solution. Entry at " + row + " ," + col + " needs a check", "ERROR");
                }
                else
                {
                    timer1.Enabled = false;
                    MessageBox.Show("Congratulations! Success Your time is  " + one2.ToString());

                }
            }
		}

		private void saveImageItem_Click(object sender, System.EventArgs e)
		{
            saveFileDialog.DefaultExt = ".jpeg";
            saveFileDialog.Filter = "JPEG Files(*.jpeg)| *.jpeg|Bitmap Files(*.bmp)|*.bmp";
            saveFileDialog.ShowDialog();
            if (gridControl != null && saveFileDialog.FileName != "")
            {              
                    gridControl.bufferBitmap.Save(saveFileDialog.FileName);
            }
            else
            {
                    gridControl1.bufferBitmap.Save(saveFileDialog.FileName);
            }


		}

		private void saveItem_Click(object sender, System.EventArgs e)
		{

            saveFileDialog.DefaultExt = ".sdk";
            saveFileDialog.Filter = "Sudoku Files(*.sdk)| *.sdk";
            saveFileDialog.ShowDialog();
            bool ansver = (saveFileDialog.FileName != "" && gridControl != null) ?
                gridControl.SaveGameToFile(Path.GetFileNameWithoutExtension(saveFileDialog.FileName) + ".sdk") :
                gridControl1.SaveGameToFile(Path.GetFileNameWithoutExtension(saveFileDialog.FileName) + ".sdk");
            if (ansver == false)
            { }
		}

		private void loadItem_Click(object sender, System.EventArgs e)
		{
           OpenFileDialog ofd = new OpenFileDialog();
                ofd.Filter = "Sudoku Files(*.sdk)| *.sdk";
                ofd.DefaultExt = ".sdk";
                ofd.ShowDialog();
                bool answer = (ofd.FileName != "" && gridControl != null) ?
                    gridControl.LoadGameFromFile(ofd.FileName) :
                    gridControl1.LoadGameFromFile(ofd.FileName);
                    changeMenuState(true);
            if(answer==false)
            {}
		}

		private void exitItem_Click(object sender, System.EventArgs e)
		{
			try
			{
               
				DialogResult dr=MessageBox.Show("Exit?","Exit?",MessageBoxButtons.YesNo);
				if(dr==DialogResult.Yes)
					Application.Exit();
				else 
					e=EventArgs.Empty;
			}
			catch
			{
				MessageBox.Show("Some error has occured to the application"
					+",Retry or close the program and start again","Error");
			}
		}

		private void manuallyGenearteItem_Click(object sender, System.EventArgs e)
		{
            gridControl1 = new GridControl_9_by_9();
            one1.Initiallize();
            DrawingTools.InitDrawingTools();
            gridControl1.Dock = DockStyle.Fill;
            gridControl1.Location = new Point(8, 8);
            gridControl1.Size = new Size(ClientSize.Width - 16, ClientSize.Height - 32);
            gridControl1.InitiallizeGrid();
            Controls.Add(gridControl1);
            gridControl1.EndGame += new EventHandler(EndGame);
            gridControl1.Enabled = false;
            changeMenuState(false);
			eraseItem.PerformClick();
			gridControl1.generatingPuzzle=true;
			MessageBox.Show("Select Problem numbers by clicking, Press DONE when 'Done'  ","Maually generate new problem");
			doneButton.Visible=true;
			gridControl1.Enabled=true;			
		    changeMenuState(false);
		}

		private void doneButton_Click(object sender, System.EventArgs e)
		{
            int row,col;
			if(!gridControl1.varify(out row,out col))
			{
				MessageBox.Show("Wrong statement. Entry at " +row+" ," +col+" needs a check","ERROR");
				return;
			}
			
			int [,] completeGrid,problemGrid;
			completeGrid=new int [one1.nbOfCells,one1.nbOfCells];
			problemGrid=new int [one1.nbOfCells,one1.nbOfCells];
			//see if problem is solvable or not
			for(row=0;row<one1.nbOfCells;row++)
			{
				for(col=0;col<one1.nbOfCells;col++)
				{
					problemGrid[row,col]=gridControl1.cellGrid[row,col].cellData.problemNb;
				}			
			}

			//if(!ProblemGenerator.hasSolution(problemGrid,ref completeGrid,true))
			//{
				DialogResult dr=MessageBox .Show("This problem is not solvable by the algorithm implemented"+
					"\nDo you still want to play ? Click YES to start playing and NO to reconfigure the problem staement ","Sorry",
					MessageBoxButtons.YesNo);
				if(dr==DialogResult.No)
				{
					completeGrid=problemGrid=null;
					return;
				}
				else
				{
					gridControl1.LoadData(completeGrid,problemGrid);					
					changeMenuState(true);
					gridControl1.generatingPuzzle=false;	
					doneButton.Visible=false;                    
					showsolutionItem.Enabled=false;
					return;
				}			    
			//}

			gridControl1.LoadData(completeGrid,problemGrid);
			MessageBox.Show("You can start playing or view the solution","Done");
			changeMenuState(true);
			gridControl1.generatingPuzzle=false;	
			doneButton.Visible=false;
		}

		private void MainForm_Closing(object sender, System.ComponentModel.CancelEventArgs e)
		{
			try
			{
				DialogResult dr=MessageBox.Show("Exit?","Exit?",MessageBoxButtons.YesNo);
				if(dr==DialogResult.Yes)
					Application.Exit();
				else 
					e.Cancel=true;
			}
			catch
			{
				MessageBox.Show("Some error has occured to the application"
					+",Retry or close the program and start again","Error");
			}
		
		}

		private void aboutItem_Click(object sender, System.EventArgs e)
		{
		}

		private void helpItem_Click(object sender, System.EventArgs e)
        {
            
		}

		private void showNextItem_Click(object sender, System.EventArgs e)
		{
            if(gridControl1!=null)
			gridControl1.showNext();
            if (gridControl != null)
                gridControl.showNext();
            			
		}

		private void changeMenuState(bool Enable)
		{
			saveItem.Enabled=Enable;
            toolsItem.Enabled=Enable;			
		}

        private void menuItem3_Click(object sender, EventArgs e)
        {
            
                    Application.Restart();

          

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
                one2 = one2.Add(two);
                label1.Text ="Time of game  "+ one2.ToString();
        }

        private void fileItem_Click(object sender, EventArgs e)
        {

        }

        
        private void menuItem4_Click(object sender, EventArgs e)
        {
            if (gridControl1 == null)
            {
                gridControl1 = new GridControl_9_by_9();
                
                one1.Initiallize();
                DrawingTools.InitDrawingTools();
                gridControl1.Dock = DockStyle.Fill;
                gridControl1.Location = new Point(8, 8);
                gridControl1.Size = new Size(ClientSize.Width - 16, ClientSize.Height - 32);
                gridControl1.InitiallizeGrid();
                Controls.Add(gridControl1);
                gridControl1.EndGame += new EventHandler(EndGame);
                gridControl1.Enabled = false;
                changeMenuState(false);
            }
            gridControl1.generatingPuzzle = false;
            doneButton.Visible = false;
            Generator_9_by_9 problemGenerator = new Generator_9_by_9();
            if (!problemGenerator.GenerateProblem())
              return;
            gridControl1.LoadData(problemGenerator.completeGrid, problemGenerator.problemGrid);
            changeMenuState(true);
            showsolutionItem.Enabled = true;
            gridControl1.Enabled = true;
            timer1.Enabled = true;
            one2 = new TimeSpan(0, 0, 0);
            two = new TimeSpan(0, 0, 1);
            
        }

       

        private void menuItem5_Click_1(object sender, EventArgs e)
        {
            if (gridControl == null)
            {
                gridControl = new GridControl_4_by_4();
                one.Initiallize();
                DrawingTools.InitDrawingTools();
                gridControl.Dock = DockStyle.Fill;
                gridControl.Location = new Point(8, 8);
                gridControl.Size = new Size(ClientSize.Width - 16, ClientSize.Height - 32);
                gridControl.InitiallizeGrid();
                Controls.Add(gridControl);
                gridControl.EndGame += new EventHandler(EndGame);
                gridControl.Enabled = false;
                changeMenuState(false);
            }
            gridControl.generatingPuzzle = false;
            doneButton.Visible = false;
            Generator_4_by_4 problemGenerator = new Generator_4_by_4();
            if (!problemGenerator.GenerateProblem())
                return;
            gridControl.LoadData(problemGenerator.completeGrid, problemGenerator.problemGrid);
            changeMenuState(true);
            showsolutionItem.Enabled = true;
            gridControl.Enabled = true;
            timer1.Enabled = true;
            one2 = new TimeSpan(0, 0, 0);
            two = new TimeSpan(0, 0, 1);

        }

     

        

       


      
        private void menuItem2_Click(object sender, EventArgs e)
        {
            int row, col;
            if (gridControl != null)
            {
                if (!gridControl.varify(out  row, out col))
                {
                    MessageBox.Show("Wrong solution. Entry at " + row + " ," + col + " needs a check", "ERROR");
                }
                else
                {
                    timer1.Enabled = false;
                    MessageBox.Show("Congratulations! Success Your time is  " + one2.ToString());


                }
            }
            if (gridControl1 != null)
            {
                if (!gridControl1.varify(out  row, out col))
                {
                    MessageBox.Show("Wrong solution. Entry at " + row + " ," + col + " needs a check", "ERROR");
                }
                else
                {
                    timer1.Enabled = false;
                    MessageBox.Show("Congratulations! Success Your time is  " + one2.ToString());

                }
            }
        }

        private void MainForm_Load(object sender, EventArgs e)
        {

        }

     

        

       

        

	}
}
